#include <sys_config.h>
#include <types.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <hld/hld_dev.h>
#include <hld/dmx/dmx_dev.h>
#include <hld/dmx/dmx.h>
#include <hld/decv/vdec_driver.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_hde.h>
#include <api/libpub29/lib_pub27.h>
#else
#include <api/libpub/lib_hde.h>
#endif


#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "images.id"
#include "string.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"
#ifdef _INVW_JUICE   
#include "control.h"
#endif

#ifdef CI_PLUS_TEST_CASE
UINT32 CIPlus_case_idx = 0xff;
UINT32 CIPlus_case_idx_spdif = 0xff;
#endif
/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern TEXT_FIELD g_win_proginput;

#define WIN_SH_IDX	WSTL_INFO_CHINPUT_01_8BIT
#ifndef SD_UI
#define W_L  120//140//60
#ifdef SUPPORT_CAS_A
#define W_T  28//50//80//90
#else
#define W_T  80//90
#endif
#define W_W  165//+30//300//120
#define W_H  54 //40
#else
#define W_L  50//140//60
#define W_T  50//90
#define W_W  123//300//120
#define W_H  44 //40
#endif

#define SUPPORT_DASH	1
#define VACT_DASH (VACT_PASS + 1)
static VACTION win_proginput_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_proginput_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

DEF_TEXTFIELD(g_win_proginput,NULL,NULL,C_ATTR_ACTIVE,2, \
    0,0,0,0,0, W_L,W_T,W_W,W_H, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    win_proginput_keymap,win_proginput_callback,  \
    C_ALIGN_RIGHT | C_ALIGN_VCENTER,30,0,0,display_strs[0])

/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/

static UINT32	input_ch_number;
static UINT8    input_cancle;
static UINT16	input_ch_high,input_ch_low;
static UINT8	cnt_input_ch_high;
static UINT8	input_dash=FALSE;
static ID		input_ch_timer = OSAL_INVALID_ID;
#define 		CHANNELINPUT_TIMER_TIME	3000  //ms
#define 		CHANNELINPUT_TIMER_NAME 	"chi"


void prog_input_timer_func(UINT unused)
{
	api_stop_timer(&input_ch_timer);
	ap_send_msg(CTRL_MSG_SUBTYPE_CMD_PLAY_PROGRAM, input_ch_number, TRUE);
}

static VACTION win_proginput_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
	case V_KEY_0:	case V_KEY_1:	case V_KEY_2:	case V_KEY_3:
	case V_KEY_4:	case V_KEY_5:	case V_KEY_6:	case V_KEY_7:
	case V_KEY_8:	case V_KEY_9:
		act = key - V_KEY_0 + VACT_NUM_0;
		break;
#if SUPPORT_DASH
	case V_KEY_REPEATAB://use repeat_ab for DASH
		input_dash = TRUE;
		act = VACT_DASH;
		break;		
#endif	
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;			
    case V_KEY_EXIT:
    case V_KEY_MENU:
        input_cancle = 1;
        act = VACT_CLOSE;
        break;
	default:
		act = VACT_PASS;		
	}

	return act;
}

static PRESULT win_proginput_unkown_act_proc(VACTION act);
#ifdef SUPPORT_CAS9
UINT32 get_mmi_showed(void);
void ShowBMailOSDOnOff ( UINT8 flag );
#endif
static PRESULT win_proginput_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT16 ch_idx = INVALID_POS_NUM;
	UINT8 back_saved;
	UINT8 av_mode;
	BOOL found = FALSE;
    UINT16 last_prog_pos = 0xffff;
    P_NODE p_node_temp;
    
	switch(	event)
	{
	case EVN_PRE_OPEN:
        #ifdef SUPPORT_CAS9
    		ShowBMailOSDOnOff(0);
    		if((0==get_mmi_showed()) && (GetSignalStatausShow()))
    			ShowSignalStatusOSDOnOff(0);
        #endif
        api_inc_wnd_count();
		input_ch_number = 0;
        input_cancle = 0;
#if SUPPORT_DASH
        input_ch_high=0;
        input_ch_low=0;
        input_dash=FALSE;
        cnt_input_ch_high=0;
#endif
		break;
	case EVN_POST_OPEN:
		input_ch_timer = api_start_timer(CHANNELINPUT_TIMER_NAME,CHANNELINPUT_TIMER_TIME,prog_input_timer_func);
		break;
	case EVN_PRE_CLOSE:
        api_stop_timer(&input_ch_timer);
		break;
	case EVN_POST_CLOSE:
#ifdef CI_PLUS_TEST_CASE
		//CIPlus test case
		CIPlus_case_idx = input_ch_number;
		CIPlus_case_idx_spdif = input_ch_number;
#endif
        if(input_cancle)
            break;
		//if(input_ch_number< get_node_num(TYPE_PROG_NODE, NULL) )
		av_mode = sys_data_get_cur_chan_mode();
        UINT8 frontend_kind=0;
        frontend_kind = get_combo_config_frontend();
#if 0                
#ifdef _LCN_ENABLE_
        if(sys_data_get_LCN() /*&& sys_data_get_cur_group_index() == get_frontend_satid(FRONTEND_TYPE_T, 0)*/)
        {
            INT16 tmp = check_LCN_index(input_ch_number);
            if(tmp >= 0)
            {
                found = TRUE;
                ch_idx = (UINT16)tmp;
#ifdef _SERVICE_ATTRIBUTE_ENABLE_
                P_NODE node;
                get_prog_at(ch_idx, &node);         
                if(!node.num_sel_flag && !node.visible_flag)
                    ch_idx = INVALID_POS_NUM;
#endif
            }
        }
        else
#endif            
        {            
		    if(input_ch_number<= get_prog_num(VIEW_ALL|av_mode, 0) )
		    {
                found = TRUE;
                if(input_ch_number != 0)
                    ch_idx = input_ch_number - 1;
		    }
        }
#endif
#ifdef _LCN_ENABLE_
        if(frontend_kind==2)
        {
            if(sys_data_get_LCN() && sys_data_get_cur_group_sat_id() == get_frontend_satid(FRONTEND_TYPE_T, 0))
            {
                INT16 tmp = check_LCN_index(input_ch_number);
                if(tmp >= 0)
                {
                    found = TRUE;
                    ch_idx = (UINT16)tmp;
#ifdef _SERVICE_ATTRIBUTE_ENABLE_
                    P_NODE node;
                    get_prog_at(ch_idx, &node);         
                    if(!node.num_sel_flag && !node.visible_flag)
                        ch_idx = INVALID_POS_NUM;
#endif
                }
            }
#if (defined(_ISDBT_ENABLE_))            
            else if(sys_data_get_LCN() && sys_data_get_cur_group_sat_id() == get_frontend_satid(FRONTEND_TYPE_T, 1))
            {
                if (check_ISDBT_index(input_ch_high,input_ch_low) >= 0)
                {
                    ch_idx = check_ISDBT_index(input_ch_high,input_ch_low);
                    
                }
            }
#endif
            else         
            {            
                if(input_ch_number<= get_prog_num(VIEW_ALL|av_mode, 0) )
                {
                    found = TRUE;
                    if(input_ch_number != 0)
                        ch_idx = input_ch_number - 1;
                }
            }    
        }
        else if(frontend_kind==1)
        {
            //this is DVBT
            if(check_frontend_type(FRONTEND_TYPE_T, 0) && sys_data_get_LCN())
            {

                INT16 tmp = check_LCN_index(input_ch_number);
                if(tmp >= 0)
                {
                    found = TRUE;
                    ch_idx = (UINT16)tmp;
#ifdef _SERVICE_ATTRIBUTE_ENABLE_
                    P_NODE node;
                    get_prog_at(ch_idx, &node);         
                    if(!node.num_sel_flag && !node.visible_flag)
                        ch_idx = INVALID_POS_NUM;
#endif
                }
            }
#if (defined(_ISDBT_ENABLE_))             
             //this is ISDBT
            else if(check_frontend_type(FRONTEND_TYPE_T, 1) && sys_data_get_LCN())
            {
                if (check_ISDBT_index(input_ch_high,input_ch_low) >= 0)
                {
                    ch_idx = check_ISDBT_index(input_ch_high,input_ch_low);
                }
            }
#endif            
            else         
            {            
                if(input_ch_number<= get_prog_num(VIEW_ALL|av_mode, 0) )
                {
                    found = TRUE;
                    if(input_ch_number != 0)
                        ch_idx = input_ch_number - 1;
                }
            }    
        }
        else
#endif        
        {
            if(input_ch_number<= get_prog_num(VIEW_ALL|av_mode, 0) )
            {
                found = TRUE;
                if(input_ch_number != 0)
                    ch_idx = input_ch_number - 1;
            }
        }
    
        
        if(ch_idx != INVALID_POS_NUM)
        {
            #ifdef MULTI_DESCRAMBLE
            last_prog_pos = sys_data_get_cur_group_cur_mode_channel();
            get_prog_at(last_prog_pos,&p_node_temp);
            //libc_printf("%s,prog_pos=%d,prog_id=%d\n",__FUNCTION__,last_prog_pos,p_node_temp.prog_id);
            /*stop pre channel's filter,ts_stream and so on*/
            #if !(defined(CAS9_V6) && defined(CAS9_PVR_SID)) // @HM_CONAX3515
            if(api_mcas_get_prog_record_flag(p_node_temp.prog_id) == FALSE)   //if the program is recording,don't stop descramble service
            #endif
            {
                //libc_printf("%s,stop service\n",__FUNCTION__);
                api_mcas_stop_service_multi_des(p_node_temp.prog_id,0xffff);
            }
            #endif
            
            ap_clear_all_message();
#ifdef MULTIFEED_SUPPORT
			P_NODE p_node;
			get_prog_at(ch_idx, &p_node);
/* 
* when change to the channel has multifeed, switch to the master feed.
* add for customer GMI.
* but the spec required that when change to the channel has multifeed,
* STB should play the last playing subfeed before user leaving that channel
*/
				/*
	        if(multifeed_have_feed(p_node.prog_id))
	        {
				multifeed_change_feed( ch_idx, 0 );
			}
			*/
#endif      

#ifdef SAT2IP_CLIENT_SUPPORT
			// Stop SAT>IP Stream
			if (api_cur_prog_is_sat2ip())//(1 == s_node.sat2ip_flag)
			{		
			//	libc_printf("\n\n%s %d\n", __FUNCTION__, __LINE__);
				win_satip_stop_play_channel();
				SatipClearTaskStatus();
			}
#endif

            api_play_channel(ch_idx, TRUE, TRUE, FALSE);

#ifdef SAT2IP_CLIENT_SUPPORT
			// Start SAT>IP Stream
			if (api_cur_prog_is_sat2ip())//(1 == s_node.sat2ip_flag)
			{
			//	libc_printf("%s %d ch:%d\n", __FUNCTION__, __LINE__, ch);
			//	win_satip_set_prog_selected(ch_idx);
				win_satip_play_channel(ch_idx, FALSE, 0, 0);
			}
#endif


#ifdef MULTIFEED_SUPPORT
	        if(multifeed_have_feed(p_node.prog_id))
	        {
	            UINT32 hk;
	            ap_vk_to_hk(0, V_KEY_RED, &hk);
	            ap_send_msg(CTRL_MSG_SUBTYPE_KEY, hk, TRUE);
	        }
			else
			{
				ap_send_msg(CTRL_MSG_SUBTYPE_CMD_ENTER_ROOT, (UINT32)&g_win_progname, FALSE);
			}
#else
#ifdef _INVW_JUICE   
			ap_send_msg(CTRL_MSG_SUBTYPE_CMD_ENTER_ROOT, (UINT32)CHANNEL_BAR_HANDLE, FALSE);

#ifdef DISABLE_ALI_INFORBAR
		            int inviewCode, retCode;
		            retCode = inviewCodeMap(V_KEY_ENTER, &inviewCode);

					INVIEW_resume(inviewCode);

					INVIEW_handleIR(inviewCode);
					//proc_ret = PROC_PASS;
#endif


#else
			ap_send_msg(CTRL_MSG_SUBTYPE_CMD_ENTER_ROOT, (UINT32)&g_win_progname, FALSE);
#endif
#endif
        }
        else if(!found)
        {
#ifdef DIAGNOSIS_OPEN
            extern CONTAINER win_diag;
            if((UINT32)9876 == input_ch_number)
                ap_send_msg(CTRL_MSG_SUBTYPE_CMD_ENTER_ROOT, (UINT32)&win_diag, TRUE);
            else
#endif
            {
            //    win_compopup_init(WIN_POPUP_TYPE_OK);
				win_compopup_init(WIN_POPUP_TYPE_SMSG);
				win_compopup_set_msg(NULL,NULL, RS_MSG_NO_SUCH_CHANNEL);
                win_compopup_open_ext(&back_saved);
				osal_task_sleep(2000);
				win_compopup_smsg_restoreback();
		 #ifdef SUPPORT_CAS7
		 win_ca_mmi_check();
		 #endif
            }
        }
        api_dec_wnd_count();
        #ifdef SUPPORT_CAS9
		restore_ca_msg_when_exit_win();
        #endif
		break;
	case EVN_UNKNOWNKEY_GOT:
		break;
	case EVN_UNKNOWN_ACTION:
		ret = win_proginput_unkown_act_proc((VACTION)(param1>>16));
		break;
	case EVN_MSG_GOT:
		if(param1 == CTRL_MSG_SUBTYPE_CMD_PLAY_PROGRAM)
			ret = PROC_LEAVE;
		break;
	default:
		;
	}

	return ret;
}

static PRESULT win_proginput_unkown_act_proc(VACTION act)
{
	PRESULT ret = PROC_LOOP;
#if SUPPORT_DASH		
	UINT8 NeedDraw=FALSE;
#endif
	TEXT_FIELD* txt;
	txt = &g_win_proginput;

	api_stop_timer(&input_ch_timer);	

        UINT8 frontend_kind=0;
        frontend_kind = get_combo_config_frontend();
	if(act>=VACT_NUM_0 && act<=VACT_NUM_9)
 #if SUPPORT_DASH
 #ifdef _LCN_ENABLE_
            if(frontend_kind==2)
            {
                 //the group is ISDBT
                if(sys_data_get_LCN() && (sys_data_get_cur_group_sat_id() == get_frontend_satid(FRONTEND_TYPE_T, 1)))
                {
        		if (FALSE == input_dash)
        		{
        			cnt_input_ch_high++;
        			if(2 < cnt_input_ch_high)
        			{
        				input_dash = TRUE;
        			}
        			else
        			{
        				input_ch_high *= 10;
        				input_ch_high += act - VACT_NUM_0;
        			}
        		}		
        		if (TRUE == input_dash)
        		{
        			input_ch_low *= 10;
        			input_ch_low += act - VACT_NUM_0;
				if(input_ch_high ==0x00)
				{
	        			if (input_ch_low > 9999)
	        				input_ch_low %= 10000;
				}
				else
				{
					if (input_ch_low > 99)
	        				input_ch_low %= 100;
				}
        		}
        		NeedDraw = TRUE;	
                }  
                else
                {
                    input_ch_number *= 10;
                    input_ch_number += act - VACT_NUM_0;
                    if(input_ch_number>9999)
                        input_ch_number %= 10000;
                    //OSD_SetTextFieldContent(txt, STRING_NUMBER, (UINT32)input_ch_number);
                    ComInt2UniStr(display_strs[0], (UINT32)input_ch_number, 4);
                    OSD_DrawObject( (POBJECT_HEAD)txt, C_UPDATE_ALL);
                    input_ch_timer = api_start_timer(CHANNELINPUT_TIMER_NAME,CHANNELINPUT_TIMER_TIME,prog_input_timer_func);
                }
            }
            else if(frontend_kind==1)
            {
                //It's just ISDBT
                if(check_frontend_type(FRONTEND_TYPE_T, 1) && sys_data_get_LCN())
                {
        		if (FALSE == input_dash)
        		{
        			cnt_input_ch_high++;
        			if(2 < cnt_input_ch_high)
        			{
        				input_dash = TRUE;
        			}
        			else
        			{
        				input_ch_high *= 10;
        				input_ch_high += act - VACT_NUM_0;
        			}
        		}		
        		if (TRUE == input_dash)
        		{
        			input_ch_low *= 10;
        			input_ch_low += act - VACT_NUM_0;
				if(input_ch_high ==0x00)
				{
	        			if (input_ch_low > 9999)
	        				input_ch_low %= 10000;
				}
				else
				{
					if (input_ch_low > 99)
	        				input_ch_low %= 100;
				}
        		}
        		NeedDraw = TRUE;	
                }  
                else
                {
                    input_ch_number *= 10;
                    input_ch_number += act - VACT_NUM_0;
                    if(input_ch_number>9999)
                        input_ch_number %= 10000;
                    //OSD_SetTextFieldContent(txt, STRING_NUMBER, (UINT32)input_ch_number);
                    ComInt2UniStr(display_strs[0], (UINT32)input_ch_number, 4);
                    OSD_DrawObject( (POBJECT_HEAD)txt, C_UPDATE_ALL);
                    input_ch_timer = api_start_timer(CHANNELINPUT_TIMER_NAME,CHANNELINPUT_TIMER_TIME,prog_input_timer_func);
                }
                
            }
            
            else
#endif     
#endif
	{
		input_ch_number *= 10;
		input_ch_number += act - VACT_NUM_0;
		if(input_ch_number>9999)
			input_ch_number %= 10000;
		//OSD_SetTextFieldContent(txt, STRING_NUMBER, (UINT32)input_ch_number);
		ComInt2UniStr(display_strs[0], (UINT32)input_ch_number, 4);
		OSD_DrawObject( (POBJECT_HEAD)txt, C_UPDATE_ALL);
		input_ch_timer = api_start_timer(CHANNELINPUT_TIMER_NAME,CHANNELINPUT_TIMER_TIME,prog_input_timer_func);
				
	}
#if SUPPORT_DASH	
	else if (act == VACT_DASH)
	{
		input_ch_low = 0;
		NeedDraw = TRUE;		
	}
#endif
	else if(act == VACT_ENTER)
	{
		ret = PROC_LEAVE;
	}

#if SUPPORT_DASH
#ifdef _LCN_ENABLE_
        if(frontend_kind==2)
        {
            //the group is ISDBT
            if(sys_data_get_LCN() && (sys_data_get_cur_group_sat_id() == get_frontend_satid(FRONTEND_TYPE_T, 1)))
            {
                if(TRUE==NeedDraw)
                {
                	input_ch_number = ((UINT32)input_ch_high*100) + input_ch_low;
                	ComInt2UniStr(display_strs[0], (UINT32)input_ch_high, 2);
                	ComAscStr2Uni("-", (UINT16*)&(display_strs[0][2]));
                	ComInt2UniStr((UINT16*)&(display_strs[0][3]), (UINT32)input_ch_low, 2);		
                	OSD_DrawObject( (POBJECT_HEAD)txt, C_UPDATE_ALL);
                	input_ch_timer = api_start_timer(CHANNELINPUT_TIMER_NAME,CHANNELINPUT_TIMER_TIME,prog_input_timer_func);
                }
            }
        }
        else if(frontend_kind==1)
        {
            //It's just ISDBT
            if(check_frontend_type(FRONTEND_TYPE_T, 1) && sys_data_get_LCN())
            {
                if(TRUE==NeedDraw)
                {
                	input_ch_number = ((UINT32)input_ch_high*100) + input_ch_low;
                	ComInt2UniStr(display_strs[0], (UINT32)input_ch_high, 2);
                	ComAscStr2Uni("-", (UINT16*)&(display_strs[0][2]));
                	ComInt2UniStr((UINT16*)&(display_strs[0][3]), (UINT32)input_ch_low, 4);		
                	OSD_DrawObject( (POBJECT_HEAD)txt, C_UPDATE_ALL);
                	input_ch_timer = api_start_timer(CHANNELINPUT_TIMER_NAME,CHANNELINPUT_TIMER_TIME,prog_input_timer_func);
                }
            }
        }
#endif      
#endif
	return ret;
	
}



